﻿/**
 @Name: Fly社区主入口
 */

layui.define(['layer', 'form', 'upload', 'layedit'], function(exports){
  
  var $ = layui.jquery
  ,layer = layui.layer
  ,form = layui.form()
  ,device = layui.device()
  ,layedit = layui.layedit;

  form.verify({
	  password: [/(.+){6,16}$/, '密码必须6到16位'],
	  confirmPwd: function(value) {
			// 获取密码
			var pwd = $("#password").val();
			if(!new RegExp(pwd).test(value)) {
				return '两次输入的密码不一致';
			}
		}
	});
  
  //阻止IE7以下访问
  if(device.ie && device.ie < 8){
    layer.alert('如果您非得使用ie浏览Fly社区，那么请使用ie8+');
  }
  
  layui.focusInsert = function(obj, str){
    var result, val = obj.value;
    obj.focus();
    if(document.selection){ //ie
      result = document.selection.createRange(); 
      document.selection.empty(); 
      result.text = str; 
    } else {
      result = [val.substring(0, obj.selectionStart), str, val.substr(obj.selectionEnd)];
      obj.focus();
      obj.value = result.join('');
    }
  };
  
  var gather = {
	   //Ajax
	   json: function(url, data, success, options){
	     var that = this;
	     options = options || {};
	     data = data || {};
	     return $.ajax({
	       type: options.type || 'post',
	       dataType: options.dataType || 'json',
	       data: data,
	       url: url,
	       success: function(res){
	       	success && success(res);
	       }, error: function(e){
	         options.error || layer.msg('请求异常，请重试', {shift: 6});
	       }
	     });
	   }
  };

  //加载特定模块
  if(layui.cache.page && layui.cache.page !== 'index') {
    var extend = {};
    extend[layui.cache.page] = layui.cache.page;
    layui.extend(extend);
    layui.use(layui.cache.page);
  }

  //手机设备的简单适配
  var treeMobile = $('.site-tree-mobile')
  ,shadeMobile = $('.site-mobile-shade')

  treeMobile.on('click', function(){
    $('body').addClass('site-mobile');
  });

  shadeMobile.on('click', function(){
    $('body').removeClass('site-mobile');
  });

  //图片懒加载
  /*
  layui.use('flow', function(flow){
    flow.lazyimg();
  });*/
  //加载编辑器
	 var layeditAdd = layedit.build('content', {
		 tool: ['strong','italic','underline','del','|','left','center','right','link','unlink','face','image' ]
	 	,uploadImage: {url: '/topic/uploadImg?dirType=' + $("#dirType").val(), type: 'post'}
	});
//表单提交
  form.on('submit(*)', function(data) {
	   data.field.content = layedit.getContent(layeditAdd);
	   data.field.categoryName = $("#categoryId").find("option:selected").text();
	   var action = $(data.form).attr('action'), button = $(data.elem);
	   // 获取指定属性，判断是json提交还是form提交
	   var jsonSub = $(data.form).attr("json-submit");
	   if(jsonSub == "false") {
		   $(data.form).submit();
	   } else {
			gather.json(action, data.field, function(res) {
				if(res && res.code && res.code == 1) {
			    	layer.msg(res.msg||"操作成功", {shift: 3, icon: 1, time: 1000},function(){window.location.href = "/"});
			} else if(res && res.msg) {
				$("#username").focus();
				layer.msg(res.msg||"操作失败", {shift: 3, icon: 5, time: 3000});
			} else {
				layer.msg("操作出错", {shift: 3, icon: 5, time: 3000});
			}
		});
    }
    return false;
  });

  exports('fly', gather);

});

